//
//  SidebarTextTransformer.m
//  SolarSeek
//
//  Created by Marcelo Alves on 03/11/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "SidebarTextTransformer.h"

@implementation SidebarTextTransformer
+ (Class)transformedValueClass {
	return [NSAttributedString class];
}

+ (BOOL)allowsReverseTransformation{
	return NO;
}

- (id)transformedValue:(id)value{
	NSManagedObject *item = (NSManagedObject *) value;
  NSString *name = [item valueForKey:@"name"];
	
	if ([item valueForKey:@"parent"]) {
		NSImage *myImage = [NSImage imageNamed:@"SmileyCry"];
		NSManagedObject *parent = (NSManagedObject *)[item valueForKey:@"parent"];
		int parentType = [[parent valueForKey:@"type"] intValue];
		if (parentType == networkType) {
			if ([name compare:@"Downloads"] == 0)
				myImage = [NSImage imageNamed:@"SidebarDownloads"];
			else if ([name compare:@"Uploads"] == 0)
				myImage = [NSImage imageNamed:@"SidebarUploads"];
			else if ([name compare:@"Friends"] == 0)
				myImage = [NSImage imageNamed:@"SidebarFriends"];
    }
		
		if (parentType == searchType)
			myImage = [NSImage imageNamed:@"SidebarSearch"];
		
		
		NSTextAttachment   *attachment  = [[[NSTextAttachment alloc] init] autorelease];
		NSCell             *cell        = (NSCell *)[attachment attachmentCell];
		[cell setImage:myImage];
		
		NSMutableAttributedString *s = [[NSMutableAttributedString alloc] init];
		NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:attachment];
		[s appendAttributedString:imageString];
		[s appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
		[s appendAttributedString:[[NSAttributedString alloc] initWithString:name]];
		
		// making the count "button"
		
    /*
		if (parentType == searchType) {
			Search * search = (Search *) item;
			
			NSMutableAttributedString *countStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"(%d)", [search.results count]]]; 
			[s appendAttributedString:countStr];
		}
		*/
		
		[s addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithFloat:4.0f] range:NSMakeRange(1, [s length] - 1)];
		
		return s;
		//[attachment release];		
	} else {
		
	}
	
	return name;//[[NSAttributedString alloc] initWithString:item.name];
}
@end
